home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / pcmag / v11n12 / dlgdemo.exe / DLGDEMO3.C next >
Text File  |  1992-04-30  |  30KB  |  784 lines

  1. //
  2. // DLGDEMO3 - Notepad Clone #3 demonstrating use of Win 3.1 Common Dialogs
  3. // Copyright (C) 1992 Ray Duncan
  4. // Ziff Davis Publishing Co. * PC Magazine
  5. //
  6.  
  7. #define WIN31
  8. #define dim(x) (sizeof(x) / sizeof(x[0]))   // returns no. of elements
  9. #define EXENAMESIZE 256                     // max length of path+filename
  10. #define BUFSIZE 65520                       // max length of file data
  11.  
  12. #include "string.h"
  13. #include "windows.h"
  14. #include "commdlg.h"
  15. #include "dlgdemo3.h"
  16.  
  17. HANDLE hInst;                               // module instance handle
  18. HWND hFrame;                                // handle for frame window
  19. HWND hEdit;                                 // handle for edit window
  20. char szFileName[EXENAMESIZE+1];             // name of current file 
  21. char szTemp[EXENAMESIZE+1];                 // filename scratch buffer
  22. int hFile;                                  // handle for current file
  23. HANDLE hBuff;                               // handle for file I/O buffer
  24. LPSTR lpBuff;                               // far pointer to file buffer
  25. DWORD dwColor = RGB(0, 0, 0);               // user-selected colorref
  26.             
  27.                                             // for FindText & ReplaceText
  28. FINDREPLACE fr;                             // common dialog data structure
  29. HWND hFindDlg = (HWND) 0;                   // nomodal dialog handle
  30. char szFind[256];                           // text to find
  31. char szReplace[256];                        // text to replace
  32.  
  33. char szShortAppName[] = "DlgDemo";          // short application name
  34. char szAppName[] = "Common Dialog Demo #3"; // long application name
  35. char szMenuName[] = "DlgDemoMenu";          // name of menu resource
  36. char szDefName[] = "UNTITLED";              // default filename
  37. char szDefExt[] = "TXT";                    // default extension
  38.  
  39. char *szFilter[] = {                        // filters for Open and
  40.     "ASCII Text (*.TXT)", "*.TXT",          // SaveAs common dialogs
  41.     "All Files (*.*)", "*.*",
  42.     "" };
  43.  
  44. struct decodeWord {                         // structure associates
  45.     WORD Code;                              // messages or menu IDs
  46.     LONG (*Fxn)(HWND, WORD, WORD, LONG); }; // with a function
  47.  
  48. //
  49. // Table of window messages supported by FrameWndProc()
  50. // and the functions which correspond to each message.
  51. //
  52. struct decodeWord messages[] = {
  53.     0, DoFindReplace,
  54.     WM_CREATE, DoCreate,
  55.     WM_INITMENU, DoInitMenu,
  56.     WM_SETFOCUS, DoSetFocus,
  57.     WM_SIZE, DoSize,
  58.     WM_COMMAND, DoCommand,
  59.     WM_DESTROY, DoDestroy, 
  60.     WM_CTLCOLOR, DoSetColor, } ;
  61.  
  62. //
  63. // Table of menubar item IDs and their corresponding functions.
  64. //
  65. struct decodeWord menuitems[] = {
  66.     IDM_NEW, DoMenuNew,
  67.     IDM_OPEN, DoMenuOpen,
  68.     IDM_SAVE, DoMenuSave,
  69.     IDM_SAVEAS, DoMenuSaveAs,
  70.     IDM_EXIT, DoMenuExit, 
  71.     IDM_UNDO, DoMenuUndo,
  72.     IDM_CUT, DoMenuCut,
  73.     IDM_COPY, DoMenuCopy,
  74.     IDM_PASTE, DoMenuPaste,
  75.     IDM_DELETE, DoMenuDelete, 
  76.     IDM_FIND, DoMenuFind,
  77.     IDM_REPLACE, DoMenuReplace,
  78.     IDM_FONT, DoMenuFont,
  79.     IDM_COLOR, DoMenuColor, } ;
  80.  
  81. //
  82. // WinMain -- entry point for this application from Windows.
  83. //
  84. int PASCAL WinMain(HANDLE hInstance,
  85.     HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  86. {
  87.     MSG msg;
  88.  
  89.     hInst = hInstance;                      // save this instance handle
  90.  
  91.     if(!hPrevInstance)                      // if first instance,
  92.         if(!InitApplication(hInstance))     // register window class
  93.         {
  94.             MessageBox(hFrame, "Can't initialize application!", szAppName, 
  95.                 MB_ICONSTOP|MB_OK);
  96.             return(FALSE);
  97.         }
  98.  
  99.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  100.     {
  101.         MessageBox(hFrame, "Can't initialize instance!", szAppName, 
  102.             MB_ICONSTOP|MB_OK);
  103.         return(FALSE);
  104.     }
  105.  
  106.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  107.     {
  108.         if((hFindDlg == 0) || !IsDialogMessage(hFindDlg, &msg))
  109.         {
  110.             TranslateMessage(&msg);         // translate virtual key codes
  111.             DispatchMessage(&msg);          // dispatch message to window
  112.         }
  113.     }
  114.  
  115.     TermInstance(hInstance);                // clean up for this instance
  116.     return(msg.wParam);                     // return code = WM_QUIT value
  117. }
  118.  
  119. //
  120. // InitApplication --- global initialization code for this application.
  121. //
  122. BOOL InitApplication(HANDLE hInstance)
  123. {
  124.     WNDCLASS  wc;
  125.  
  126.     // set parameters for frame window class
  127.     wc.style = CS_HREDRAW|CS_VREDRAW;       // class style
  128.     wc.lpfnWndProc = FrameWndProc;          // class callback function
  129.     wc.cbClsExtra = 0;                      // extra per-class data
  130.     wc.cbWndExtra = 0;                      // extra per-window data
  131.     wc.hInstance = hInstance;               // handle of class owner
  132.     wc.hIcon = LoadIcon(hInst, "DlgDemoIcon");      // application icon
  133.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  134.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  135.     wc.lpszMenuName =  szMenuName;          // name of menu resource
  136.     wc.lpszClassName = szShortAppName;      // name of window class
  137.  
  138.     return(RegisterClass(&wc));             // register class, return status
  139. }
  140.  
  141. //
  142. // InitInstance --- instance initialization code for this application.
  143. //
  144. BOOL InitInstance(HANDLE hInstance, WORD nCmdShow)
  145. {
  146.     hFrame = CreateWindow(                  // create frame window
  147.         szShortAppName,                     // window class name
  148.         szAppName,                          // text for title bar
  149.         WS_OVERLAPPEDWINDOW,                // window style
  150.         CW_USEDEFAULT, CW_USEDEFAULT,       // default position
  151.         CW_USEDEFAULT, CW_USEDEFAULT,       // default size
  152.         NULL,                               // no parent window
  153.         NULL,                               // use class default menu
  154.         hInstance,                          // window owner
  155.         NULL);                              // unused pointer
  156.  
  157.     if(!hFrame) return(FALSE);              // error, can't create window
  158.  
  159.     hBuff = GlobalAlloc(GMEM_MOVEABLE, BUFSIZE);    // allocate memory
  160.     if(!hBuff)                                      // abort if no memory
  161.     {
  162.         MessageBox(hFrame, "Can't allocate memory!", szAppName, 
  163.             MB_ICONSTOP|MB_OK);
  164.         return(0);
  165.     }
  166.  
  167.     lpBuff = GlobalLock(hBuff);             // get far pointer to memory
  168.  
  169.     // register message for FindText() and ReplaceText() common dialogs
  170.     messages[0].Code = RegisterWindowMessage((LPSTR) FINDMSGSTRING);
  171.  
  172.     ShowWindow(hFrame, nCmdShow);           // make frame window visible
  173.     UpdateWindow(hFrame);                   // force WM_PAINT message
  174.     SendMessage(hFrame, WM_COMMAND,         // force new (empty) file by 
  175.         IDM_NEW, 0L);                       // simulating File-New command
  176.     return(TRUE);                           // return success flag
  177. }
  178.  
  179. //
  180. // TermInstance -- instance termination code for this application.
  181. //
  182. BOOL TermInstance(HANDLE hinstance)
  183. {
  184.     GlobalUnlock(hBuff);                    // unlock the memory buffer
  185.     GlobalFree(hBuff);                      // release the buffer
  186.     return(TRUE);                           // return success flag
  187. }
  188.  
  189. //
  190. // FrameWndProc --- callback function for application frame window.
  191. //
  192. LONG FAR PASCAL FrameWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  193. {
  194.     int i;                                  // scratch variable
  195.  
  196.     for(i = 0; i < dim(messages); i++)      // decode window message and
  197.     {                                       // run corresponding function
  198.         if(wMsg == messages[i].Code)
  199.             return((*messages[i].Fxn)(hWnd, wMsg, wParam, lParam));
  200.     }
  201.  
  202.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  203. }
  204.  
  205. //
  206. // DoCreate -- process WM_CREATE message for frame window by
  207. // creating a multiline edit control that will fill the frame window.
  208. // 
  209. LONG DoCreate(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  210. {
  211.     hEd